package team.seekers.hula.island.post.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.common.entity.VisibilityUpdateDTO;
import team.seekers.hula.common.entity.enums.WindTypeEnum;
import team.seekers.hula.common.utils.BaseConvertUtils;
import team.seekers.hula.island.post.dao.IslandPostDao;
import team.seekers.hula.island.post.entity.vo.IslandPostManageListVO;
import team.seekers.hula.record.wind.service.WindService;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.island.post.entity.dao.IslandPostDO;
import team.seekers.hula.island.post.entity.dto.IslandPostPageDTO;
import team.seekers.hula.island.post.entity.dto.IslandPostManageUpdateDTO;
import team.seekers.hula.island.postComment.entity.dto.PostCommentListDTO;
import team.seekers.hula.island.postComment.service.IslandPostCommentService;
import team.seekers.hula.user.user.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ekin
 * @Description GOOD
 * @mood happy
 */
@Service
public class IslandPostManageService{

    @Autowired
    private UserService userService;

    @Autowired
    private IslandPostDao islandPostDao;

    @Autowired
    private IslandPostCommentService islandPostCommentService;

    @Autowired
    private WindService windService;

    public PageResult<IslandPostManageListVO> pagePost(IslandPostPageDTO islandPostPageDTO) {
        LambdaQueryChainWrapper<IslandPostDO> wrapper = islandPostDao.lambdaQuery();

        if(!StringUtils.isEmpty(islandPostPageDTO.getNickname())){
            List<String> userIds =  userService.listIdsLikeNickname(islandPostPageDTO.getNickname());
            if(userIds != null && userIds.size() > 0){
                wrapper.in(IslandPostDO::getUserId,userIds);
            }else {
                return new PageResult<>();
            }
        }

        //帖子内容模糊查询
        if(!StringUtils.isEmpty(islandPostPageDTO.getContent())){
            wrapper.like(IslandPostDO::getContent, islandPostPageDTO.getContent());
        }

        if(!StringUtils.isEmpty(islandPostPageDTO.getUserId())){
            wrapper.eq(IslandPostDO::getUserId, islandPostPageDTO.getUserId());
        }

        if(!StringUtils.isEmpty(islandPostPageDTO.getIslandId())){
            wrapper.eq(IslandPostDO::getIslandId, islandPostPageDTO.getIslandId());
        }

        if(!StringUtils.isEmpty(islandPostPageDTO.getId())){
            wrapper.eq(IslandPostDO::getId, islandPostPageDTO.getId());
        }

        if(!StringUtils.isEmpty(islandPostPageDTO.getType())){
            wrapper.eq(IslandPostDO::getType, islandPostPageDTO.getType());
        }

        if(!StringUtils.isEmpty(islandPostPageDTO.getVisibility())){
            wrapper.eq(IslandPostDO::getVisibility, islandPostPageDTO.getVisibility());
        }

        wrapper.orderByDesc(IslandPostDO::getGmtCreate);

        Page<IslandPostDO> page = wrapper.page(new Page<>(islandPostPageDTO.getPageNow(), islandPostPageDTO.getPageSize()));
        if(page.getRecords().size() == 0){
            return new PageResult<>();
        }

        List<IslandPostManageListVO> list = new ArrayList<>();
        for (IslandPostDO islandPostDO : page.getRecords()){
            IslandPostManageListVO islandPostListVO = new IslandPostManageListVO();
            BeanUtils.copyProperties(islandPostDO,islandPostListVO);
            islandPostListVO.setPictures(BaseConvertUtils.stringToList(islandPostDO.getPictures()));
            islandPostListVO.setUserInfo(userService.getBasicInfoVO(islandPostDO.getUserId()));
            if(islandPostDO.getVisibility() != 0){
                islandPostListVO.setComments(islandPostCommentService.getMyComment(PostCommentListDTO.builder().postId(islandPostDO.getId()).userId(MindUtils.getUserId()).build()));
            }
            list.add(islandPostListVO);
        }

        return new PageResult<>(page.getTotal(),list);
    }

    public void updatePost(IslandPostManageUpdateDTO updateDTO) {
        IslandPostDO islandPostDO = islandPostDao.getById(updateDTO.getId());
        BeanUtils.copyProperties(updateDTO, islandPostDO);
        islandPostDao.updateById(islandPostDO);
    }

    public void updateVisibility(VisibilityUpdateDTO updateDTO) {
        islandPostDao.lambdaUpdate()
                .set(IslandPostDO::getVisibility,updateDTO.getVisibility())
                .eq(IslandPostDO::getId,updateDTO.getId()).update();

        Integer type = islandPostDao.lambdaQuery().select(IslandPostDO::getType).eq(IslandPostDO::getId, updateDTO.getId()).one().getType();

        windService.updateVisibility(WindTypeEnum.getByType(type+1),updateDTO.getId(),updateDTO.getVisibility());
    }
}
